using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class ProduceOrderController : GenericController {

        public ActionResult Index() {
            return View();
        }

        [HttpPost]
        public ActionResult Index(JQGridParm parm) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.ProduceOrderRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.ProduceOrderRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters);

            var enums = entities.Select(e => new {
                id = e.ProduceOrderId,
                cell = new object[] { 
                    e.Number,
                    e.Name,
                    e.Date.ToDate(),
                    e.Count,
                    e.InDepositoryCount,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }
        [HttpPost]
        public ActionResult OutDepositoryIndex(JQGridParm parm, int id) {
            string sidx = parm.Sidx;
            string sord = parm.Sord;
            int page = parm.Page;
            int rows = parm.Rows;

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = unitOfWork.ProduceOrderRepository.dbSet.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var entities = unitOfWork.OutDepositoryRepository.Get(0, 100, "OutDepositoryId", "asc", parm.Filters, o => o.ProduceOrderId == id);
            var enums = entities.Select(e => new {
                id = e.OutDepositoryId,
                cell = new object[] { 
                    e.ProductValueObject.Number,
                    e.ProductValueObject.Name,
                    e.ProductValueObject.Spec,
                    e.ProductValueObject.Model,
                    e.ProductValueObject.Unit,
                    e.SingleCount,
                    e.ShouldCount,
                    e.RealCount,
                    e.Date.ToDate(),
                    e.Number,
                    e.Remark
                }
            });

            var jsonData = new {
                success = true,
                total = totalPages,
                page,
                records = totalRecords,
                rows = enums
            };
            return Json2(jsonData);
        }
        [HttpPost]
        public ActionResult Add(ProduceOrder produceorder) {
            ProduceOrder produceorderAdd = new ProduceOrder();
            if (TryUpdateModel(produceorderAdd)) {
                unitOfWork.ProduceOrderRepository.Add(produceorderAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, ProduceOrder produceorder) {
            if (id != null) {
                ProduceOrder produceorderEdit = unitOfWork.ProduceOrderRepository.Get((int)id);
                if (produceorderEdit != null) {
                    if (TryUpdateModel(produceorderEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.ProduceOrderRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }

        public ActionResult ProduceOrders() {
            var produceorders = unitOfWork.ProduceOrderRepository.dbSet.Select(s => new { key = s.ProduceOrderId, value = s.Name }).AsEnumerable();
            var jsonData = new { value = produceorders };
            return Json2(jsonData);
        }

        public ActionResult GetProduceOrder(int id) {
            var produceOrder = unitOfWork.ProduceOrderRepository.dbSet.Include("OutDepositories").FirstOrDefault(p=>p.ProduceOrderId == id);
            if(produceOrder == null)
                return Json2(new { success = false, message = errorMessage });

            var items = produceOrder.OutDepositories.Select(item => new object[]{
                item.ProductValueObject.Number,
                item.ProductValueObject.Name,
                item.ProductValueObject.Spec,
                item.ProductValueObject.Model,
                item.ProductValueObject.Unit,
                item.SingleCount,
                item.ShouldCount,
                item.RealCount,
                item.Date.ToDate(),
                item.Number,
                item.Remark
            });
            var jsonData = new { 
                success = true,

                number = produceOrder.Number,
                name = produceOrder.Name,
                count = produceOrder.Count,

                outDepositories = items
            };
            return Json2(jsonData);
        }
    }
}

